package com.blogs.component;

import java.util.ArrayList;

import com.blogs.entity.NewsFeed;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.FormatTime;
import com.cnblogs.R;
import com.cnblogs.R.color;


import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter{
	private  ArrayList<NewsFeed> feedList = null;
	private ViewHolder holder;
	private Activity context = null;
	private DBSharedPreferences sp;
	public NewsAdapter(Activity context, ArrayList<NewsFeed> feedList)
	{
		this.context = context;
		this.feedList = feedList;
		sp= new DBSharedPreferences(context);
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return feedList.size();
	}
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return feedList.get(position);
	}
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null ) {
			convertView = View.inflate(context, R.layout.home_adapter_template,null);
			holder=new ViewHolder();
			holder.home_adapter_title_tv=(TextView)convertView.findViewById(R.id.home_adapter_title_tv);
			holder.home_adapter_con_tv=(TextView)convertView.findViewById(R.id.home_adapter_con_tv);
			holder.home_adapter_author_tv=(TextView)convertView.findViewById(R.id.home_adapter_author_tv);
			holder.home_adapter_public_time_tv=(TextView)convertView.findViewById(R.id.home_adapter_public_time_tv);
			holder.home_adapter_comment_tv=(TextView)convertView.findViewById(R.id.home_adapter_comment_tv);
			holder.home_adapter_hit_tv=(TextView)convertView.findViewById(R.id.home_adapter_hit_tv);
			convertView.setTag(holder);
		}
	    else
	    {
	    	holder = (ViewHolder) convertView.getTag();
	    }
		holder.home_adapter_title_tv.setText(feedList.get(position).title);
		holder.home_adapter_con_tv.setText(feedList.get(position).content);
		holder.home_adapter_author_tv.setText(feedList.get(position).author);
		holder.home_adapter_public_time_tv.setText(FormatTime.format( feedList.get(position).public_time));
		holder.home_adapter_comment_tv.setText(feedList.get(position).comment);
		holder.home_adapter_hit_tv.setText(feedList.get(position).hit);
		if(sp.GetIsDarkState())
		{
			holder.home_adapter_con_tv.getBackground().setLevel(2);
			holder.home_adapter_con_tv.setTextColor(context.getResources().getColor(color.con_text_night));
		}
		else
		{
			holder.home_adapter_con_tv.getBackground().setLevel(1);
			holder.home_adapter_con_tv.setTextColor(context.getResources().getColor(color.con_text_light));
		}
		Log.i("ListView_position", position+"");
		return convertView;
	}
	
	class ViewHolder 
	{
		TextView home_adapter_title_tv;
		TextView home_adapter_con_tv;
		TextView home_adapter_author_tv;
		TextView home_adapter_public_time_tv;
		TextView home_adapter_comment_tv;
		TextView home_adapter_hit_tv;
	}
}
